//自定义触发mutations里函数的方法，context与store 实例具有相同方法和属性


import * as types from './mutation-types'
import {playMode} from "../common/js/config";
import {shuffle} from "../common/js/utils";
import {saveSearch,deleteSearch,clearSearch,savePlay,saveFavorite,deleteFavorite} from "../common/js/cache";


function findIndex(list,song) {
  return list.findIndex((item)=>{
    return item.id === song.id
  })
}
//点击播放时需要一些数据 ========= 封装这些数据多处调用
//{commit,state} 结构可以是commit方法和state属性的action对象
export const selectPlay = function ({commit,state},{list,index}) {
  //提交mutation========
  //顺序播放
  commit(types.SET_SEQUENCE_LIST,list)
  if(state.mode === playMode.random){
    //随机播放
    const randomList = shuffle(list)
    commit(types.SET_PLAYLIST,randomList)
    //顺序列表当前的歌对应随机播放列表那首歌的索引
    index = findIndex(randomList,list[index])
  }else{
    //顺序播放
    commit(types.SET_PLAYLIST,list)
  }
  commit(types.SET_CURRENT_INDEX,index)
  //打开播放器
  commit(types.SET_FULL_SCREEN,true)
  //播放状态
  commit(types.SET_PLAYING_STATE,true)

}
export const randomPlay = function ({commit,state},{list}) {
  commit(types.SET_PLAY_MODE,playMode.random)
  commit(types.SET_SEQUENCE_LIST,list)
  const randomList = shuffle(list)
  commit(types.SET_PLAYLIST,randomList)
  commit(types.SET_CURRENT_INDEX,0)
  commit(types.SET_FULL_SCREEN,true)
  commit(types.SET_PLAYING_STATE,true)
}

export const insertSong = function ({commit,state},song) {
  // Error in callback for watcher "function () { return this._data.$$state }": "Error: [vuex] Do not mutate vuex store state outside mutation handler===
  //因为引入state.playlist，在底下直接修改，不能直接修改，必须在mutation的回调函数中修改，添加一个副本就可以修改了
  //slice()添加一个副本
  let playlist = state.playlist.slice()
  let sequenceList = state.sequenceList.slice()
  let currentIndex = state.currentIndex
  //记录当前歌曲
  let currentSong = playlist[currentIndex]
  //查找当前列表是否有待插入的歌曲并返回索引
  let fpIndex = findIndex(playlist,song)
  //因为是插入歌曲，所以索引+1
  currentIndex++
  //插入这首歌到当前索引位置
  playlist.splice(currentIndex,0 ,song)
  //如果已经包含了这首歌
  if(fpIndex>-1){
    //如果当前插入的序号大于列表中的序号 [1,2,3,4]  要在4后面加入2.就要把之前的2给删除，索引就要-1
    if(currentIndex> fpIndex){
      playlist.splice(fpIndex,1)
      currentIndex--
    }else{
      //[1,3,4,2]要在3之前加2，把后面的2给删除，因为加入了一个数，删除的时候就要加1
      playlist.splice(fpIndex+1,1)
    }
  }
  let currentSIndex = findIndex(sequenceList, currentSong) + 1

  let fsIndex = findIndex(sequenceList, song)

  sequenceList.splice(currentSIndex, 0, song)

  if (fsIndex > -1) {
    if (currentSIndex > fsIndex) {
      sequenceList.splice(fsIndex, 1)
    } else {
      sequenceList.splice(fsIndex + 1, 1)
    }
  }
  commit(types.SET_PLAYLIST, playlist)
  commit(types.SET_SEQUENCE_LIST, sequenceList)
  commit(types.SET_CURRENT_INDEX, currentIndex)
  commit(types.SET_FULL_SCREEN, true)
  commit(types.SET_PLAYING_STATE, true)
}


//历史记录缓存的封装
export const saveSearchHistory = function ({commit,state},query) {
  commit(types.SET_SEARCH_HISTORY,saveSearch(query))
}

//删除一条记录
export const deleteSearchHistory = function ({commit,state},query) {
  commit(types.SET_SEARCH_HISTORY,deleteSearch(query))
}

//删除全部记录
export const clearSearchHistory = function ({commit}) {
  commit(types.SET_SEARCH_HISTORY,clearSearch())
}

//播放列表中删除一条记录
export const deleteSong = function ({commit, state}, song) {
  let playlist = state.playlist.slice()
  let sequenceList = state.sequenceList.slice()
  let currentIndex = state.currentIndex
  let pIndex = findIndex(playlist, song)
  playlist.splice(pIndex, 1)
  let sIndex = findIndex(sequenceList, song)
  sequenceList.splice(sIndex, 1)
  if (currentIndex > pIndex || currentIndex === playlist.length) {
    currentIndex--
  }
  commit(types.SET_PLAYLIST, playlist)
  commit(types.SET_SEQUENCE_LIST, sequenceList)
  commit(types.SET_CURRENT_INDEX, currentIndex)

  if (!playlist.length) {
    commit(types.SET_PLAYING_STATE, false)
  } else {
    commit(types.SET_PLAYING_STATE, true)
  }
}

//清空播放列表
export const deleteSongList = function ({commit}) {
  commit(types.SET_CURRENT_INDEX, -1)
  commit(types.SET_PLAYLIST, [])
  commit(types.SET_SEQUENCE_LIST, [])
  commit(types.SET_PLAYING_STATE, false)
}

//最近播放==========播放列表封装
export const savePlayHistory = function ({commit},song) {
  commit(types.SET_PLAY_HISTORY,savePlay(song))
}

//用户中心收藏====存
export const saveFavoriteList = function ({commit}, song) {
  commit(types.SET_FAVORITE_LIST, saveFavorite(song))
}
//用户中心收藏====删
export const deleteFavoriteList = function ({commit}, song) {
  commit(types.SET_FAVORITE_LIST, deleteFavorite(song))
}

